﻿using System;
namespace AEM.Data.BLL.Actions
{
    public class FaultDefinition
    {
        static FaultDefinition()
        {
    
            AddFault = doAddFault;
            ModifiyFault = doModifiyFault;
            DeleteFault = doDeleteFault;
         }

        //======================================================================================================================
        #region 故障信息添加
        private static AddFaultResult doAddFault(Model.FaultDefinition faultinfo)
        {
            if (null == faultinfo) return AddFaultResult.FaultIsNull;
            Model.FaultDefinition model = DAL.FaultDefinition.Select(faultinfo.ID);
            if (null!=model) return AddFaultResult.FaultIsExist;
            model = null;
            model=DAL.FaultDefinition.Select("FaultName",faultinfo.FaultName);
            if (null!=model) return AddFaultResult.FaultNameIsExist;
            model = null;
            model = DAL.FaultDefinition.Select("FaultCode",faultinfo.FaultCode);
            if (null!=model) return AddFaultResult.FaultCodeExist;
            return DAL.FaultDefinition.Add(faultinfo);
           
        }
        #endregion 错误信息添加
        #region 故障信息修改
        private static ModifiyFaultResult doModifiyFault(Model.FaultDefinition args)
        {
            if (null == args) return ModifiyFaultResult.FaultIsNull;
            Model.FaultDefinition model = DAL.FaultDefinition.Select(args.ID);
            if (null==model) return ModifiyFaultResult.FaultIsNotExist;

            return DAL.FaultDefinition.Update(args);
        }
        #endregion 错误信息修改
        #region 删除故障信息
        public static DeleteFaultResult doDeleteFault(int id)     
        {
            Model.FaultDefinition model = DAL.FaultDefinition.Select(id);
            if (null==model) return DeleteFaultResult.FaultIsNotExist;

            bool ret = DAL.FaultDefinition.Delete(id);
            if (ret)
                return DeleteFaultResult.OK;
            else
                return DeleteFaultResult.Error;
        } 
        #endregion 删除错误信息
        //=======================================================================================================================


        #region 函数定义
        /// <summary>
        /// 添加故障信息
        /// 第一个参数：故障信息
        /// 第二个参数:返回
        /// </summary>
        public static Func<Model.FaultDefinition, AddFaultResult> AddFault;
        public static Func<Model.FaultDefinition, ModifiyFaultResult> ModifiyFault;
        public static Func<int, DeleteFaultResult> DeleteFault;
        #endregion 函数定义

        public enum AddFaultResult
        {
            OK,
            FaultIsExist,
            FaultIsNull,
            FaultNameIsExist,
            FaultCodeExist
        }
        public enum ModifiyFaultResult
        {
            OK,
            /// <summary>
            /// 故障内容信息为空
            /// </summary>
            FaultIsNull,
            /// <summary>
            /// 错误不存在
            /// </summary>
            FaultIsNotExist,
            /// <summary>
            //错误名已经存在
            /// </summary>          
            FaultNameAlreadyExist
        }
        public enum DeleteFaultResult
        {
            OK,
            FaultIsNotExist,
            Error
        }
    }
}
